package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BillAttach;
import com.open.capacity.reimburse.dao.BillAttachDao;
import com.open.capacity.reimburse.service.BillAttachService;


@Service
public class BillAttachServiceImpl  implements BillAttachService {

    @Autowired
    private BillAttachDao billAttachDao;

    /**
     * 添加
     * @param billAttach
     */
    public int save(BillAttach billAttach){
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        billAttach.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billAttach.setCreateUserid(loginUser.getId());
        billAttach.setCreateUsername(loginUser.getRealName());
        billAttach.setCreateTime(new Date());
    	  return billAttachDao.save(billAttach);
    }

    /**
     * 修改
     * @param billAttach
     */
    public int update(BillAttach billAttach){
        return billAttachDao.update(billAttach);
    }

    @Override
    public int deleteByBillId(String billId) {
        return billAttachDao.deleteByBillId(billId);
    }


    /**
     * 删除
     * @param id
     */
    public int delete(String id){
        return billAttachDao.delete(id);
    }

    /**
     * 查询记录
     * @param id
     * @return
     */
    public BillAttach findById(String id){
        return billAttachDao.findById(id);
    }

    @Override
    public List<BillAttach> findByBillId(String billId) {
        return billAttachDao.findByBillId(billId);
    }

    /**
     * 分页
     * @param params
     * @return
     */
    public PageResult<BillAttach> pageAll(Map<String, Object> params){
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page")!=null && MapUtils.getInteger(params, "limit")!=null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"),MapUtils.getInteger(params, "limit"),true);

        List<BillAttach> list  =  billAttachDao.findAll(params);
        PageInfo<BillAttach> pageInfo = new PageInfo(list);

        return PageResult.<BillAttach>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
    * 列表
    * @param params
    * @return
    */
    public List<BillAttach> findAll(Map<String, Object> params){
        return  billAttachDao.findAll(params);
    }

}
